﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DALImplementation;
using MaxLeaf.Platform.DataContract;
using CommonsLib.Utility;
using CommonsLib.Settings;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.BRL;
using MaxLeaf.Platform.Commons;
using CommonsLib.DB;
using System.Transactions;

namespace MaxLeaf.Platform.BLLImplementation
{
    public class imUsingListProcessor : IUsingListProcess
    {
        
         

        /// <summary>
        /// 提交为申请单
        /// </summary>
        /// <param name="intDocStatus"></param>
        /// <param name="lstUsingListKeys"></param>
        /// <returns></returns>
        public imStoresReqHdrDataModel Post(int intDocType, int intDocStatus, List<int> lstUsingListKeys)
        {
            if (lstUsingListKeys == null || lstUsingListKeys.Count == 0)
            {
                return null;
            }

            imUsingListBLL bllUsingList = new imUsingListBLL();

            BaseBLL bllReqHdr=BaseBLL.GetDataModelBLL("imStoresReqHdrDataModel");
            imStoresReqDtlBLL bllReqDtl = new imStoresReqDtlBLL();

            imStoresReqHdrDataModel modelHdr =null;

            List<imStoresReqDtlDataModel> lstDtl = new List<imStoresReqDtlDataModel>();
            for (int i = 0; i < lstUsingListKeys.Count; i++)
            {
                imUsingListDataModel modelUsingList = new imUsingListDataModel();
                modelUsingList = (imUsingListDataModel)bllUsingList.Select(modelUsingList, lstUsingListKeys[i]);
                
                if(i==0)
                {
                    int intApprovalCategorySeqNo = intDocType;

                    //LogManager.Debug("DocType:" + intDocType.ToString() );
                    //LogManager.Debug("ApprovalCategorySeqNo:" + intApprovalCategorySeqNo.ToString()); 
                    int intDepID = 0;
                    using (DBAccess dbcon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase)))
                    {
                        intDepID = CvtFunc.ToInt(DataFunc.GetFieldValue(dbcon,"mUser_Info","DepID",new Dictionary<string,object>{{"UserID",modelUsingList.UserID}}));
                        string strSQL = "select count(*) from SCPCategory where Companyid='" + modelUsingList.CompanyID + "' and SCPCategorySeqNo='-1' and Is_Active=1";
                        if ( CvtFunc.ToInt ( dbcon.GetValue( strSQL ))>0)  
                        {
                            intApprovalCategorySeqNo = -1;
                        }
                        //LogManager.Debug("ApprovalCategorySeqNo:" + intApprovalCategorySeqNo.ToString()); 
                    }
                    modelHdr = Post_InitHdr(bllReqHdr, modelUsingList, intDepID, intDocType,intApprovalCategorySeqNo, intDocStatus);
                    //LogManager.Debug("ApprovalCategorySeqNo:" + intApprovalCategorySeqNo.ToString()); 
                    
                }

                lstDtl.Add(Post_InitDtl(bllReqDtl, modelHdr.Key, modelUsingList));
 
            }

            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
                using (DBAccess dbConOrder = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPSEOrder)))
                {
                    try
                    {
                        //dbConOrder.BeginTrans();
                        modelHdr = (imStoresReqHdrDataModel)bllReqHdr.Update(dbConOrder, modelHdr);


                        for (int i = 0; i < lstUsingListKeys.Count; i++)
                        {
                            if (bllUsingList.CurrentBRL.CanPost(dbConOrder, bllUsingList.CurrentDAL, "imUsingList", "FInterID", lstUsingListKeys[i]))
                            {
                                lstDtl[i] = (imStoresReqDtlDataModel)bllReqDtl.Update(dbConOrder, lstDtl[i]);
                                //写入来源 usinglist key
                                bllReqDtl.SetUsingListId(dbConOrder, lstDtl[i].Key, lstUsingListKeys[i]);

                                bllUsingList.SetPosted(dbConOrder, lstUsingListKeys[i], lstDtl[i].ForeignKey, lstDtl[i].Key);
                            }

                        }
                        //dbConOrder.CommitTrans();
                    }
                    catch(Exception ex)
                    {
                        //dbConOrder.RollbackTrans();
                        LogManager.Debug(ex.Message);
                        throw;
                    }
                }

                ts.Complete();
            }
            

            return modelHdr;
        }



        private imStoresReqHdrDataModel Post_InitHdr(BaseBLL bllReqHdr, imUsingListDataModel modelUsingList, int intDepID, int intDocType, int intApprovalCategorySeqNo, int intDocStatus)
        {
            imStoresReqHdrDataModel modelHdr = new imStoresReqHdrDataModel(
                modelUsingList.CompanyID
                , modelUsingList.UserID
                , intDepID
                , intDocType
                , intApprovalCategorySeqNo
                , intDocStatus               
                , DateTime.Now);
            return (imStoresReqHdrDataModel)bllReqHdr.InsertFinterID(modelHdr);
 
        }

        private imStoresReqDtlDataModel Post_InitDtl(BaseBLL bllReqDtl, int intForeignKey, imUsingListDataModel modelUsingList)
        {
            if (modelUsingList.ReqQty.CompareTo(0d) <= 0)
            {
                throw new MaxLeafException("数量不能为0");
            }

            imStoresReqDtlDataModel modelDtl = new imStoresReqDtlDataModel(
                intForeignKey
                , modelUsingList.ItemID
                , modelUsingList.UnitID
                , modelUsingList.ReqQty
                , modelUsingList.Price
                , modelUsingList.Subtotal
                , modelUsingList.WebPrice
                );

            return (imStoresReqDtlDataModel)bllReqDtl.InsertFinterID(modelDtl);

  
        }

         
 
    }
}
